<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class QuyensController extends AppController {
    var $name = "Quyens";
    var $helpers = array('Html','Session');
    // var $components = array( 'Session', 'Common', 'Auth');
    var $components = array( 'Session', 'Common');

    function beforeFilter(){
        // parent::beforeFilter();
        $controller = $this->params['controller'];
        $action = $this->params['action'];
        if(isset($controller)){
            $strControllerName =  "<a href='/admin/" .$controller. "'>Phân quyền người dùng vào nhóm</a>";
        }
        if(isset($action)){
            if($action == "admin_them"){
                $strActionName = " » Thêm dịch vụ";
            }elseif($action == "admin_sua"){
                $strActionName = " » Chỉnh sửa dịch vụ";
            }
            elseif ($action == "admin_xoa") {
                $strActionName = " » Xóa dịch vụ";
            }else{
               $strActionName = "";
            }
        }

        //Add breadcrumb
        $route = $strControllerName. $strActionName;

        $status = array(
            'hoatdong' => 'Hoạt động',
            'vohieu' => 'Vô hiệu'
        );

        $this->set('title_for_layout','Quản trị dịch vụ');
        $this->set('strControllerName',$controller);
        $this->set('status', $status);
        $this->set('route', $route);
        $this->set('group_system', 1);
    }

    public function admin_index(){
        if( !empty($this->data) )
        {
            if( strlen( $this->data['Quyen']['nhomid'] ) == 0 )
            {
                $this->Session->write( 'message_error', 'Xin chọn nhóm.' );
            }

            if( strlen( $this->data['Quyen']['menuid'] ) == 0)
            {
                $this->Session->write( 'message_error', 'Xin chón menu.' );
            }

            if( strlen( $this->data['Quyen']['nhomid'] ) > 0 && strlen( $this->data['Quyen']['menuid'] ) > 0 ){
                $this->Session->write( 'Quyen_nhomid', $this->data['Quyen']['nhomid'] );
                $this->Session->write( 'Quyen_menuid', $this->data['Quyen']['menuid'] );
                $this->loadModel( 'Menu' );
                $arr_Menu = $this->Menu->find('list', array(
                            'fields' => array('id', 'tenmenu'),
                            'conditions' => array(
                                'Menu.trangthai' => 'hoatdong',
                                'Menu.menucha' => $this->data['Quyen']['menuid']
                            ),
                            'contain' => false
                        ));
                foreach( $arr_Menu as $key => $value ){
                    $arr_Quyen = $this->Quyen->find('all', array(
                        'conditions' => array(
                            'Quyen.trangthai' => 'hoatdong',
                            'Quyen.menuid' => $key,
                            'Quyen.nhomid' => $this->data['Quyen']['nhomid']
                        ),
                        'contain' => false
                    ));

                    if( !empty($arr_Quyen) ){
                        $this->Quyen->delete($arr_Quyen[0]['Quyen']['id']);
                    }
                }

                foreach ($arr_Menu as $key => $value) {
                    $save = 0;
                    if( isset($this->data['Quyen']['save']) ){
                        foreach($this->data['Quyen']['save'] as $keysave => $value1){
                            if($key == $keysave){
                                $save = 1;
                                break;
                            }
                        }
                    }

                    $edit = 0;
                    if( isset($this->data['Quyen']['edit']) ){
                        foreach($this->data['Quyen']['edit'] as $keyedit => $value2){
                            if($key == $keyedit){
                                $edit = 1;
                                break;
                            }
                        }
                    }

                    $delete = 0;
                    if( isset($this->data['Quyen']['delete']) ){
                        foreach($this->data['Quyen']['delete'] as $keydelete => $value3){
                            if($key == $keydelete){
                                $delete = 1;
                                break;
                            }
                        }
                    }

                    $sql = 'insert into quyens(`nhomid`,`menuid`,`xem`,`xoa`,`sua`,
                        `nguoitao`,`ngaytao`,`nguoisua`,`ngaysua`,`trangthai`) values ('
                        . $this->data['Quyen']['nhomid'] . ',' . $key . ',' .
                        $save . ',' . $delete . ',' . $edit . ',' . $this->Session->read('nguoidung_id')
                            . ',"' . date('Y-m-d H:i:s') . '",' . $this->Session->read('nguoidung_id')  . ',"'
                            . date('Y-m-d H:i:s') . '","hoatdong")';
                    $this->Quyen->query($sql);
                }
            }

        }
        $this->loadModel( 'Nhom' );
        $arr_Nhom = $this->Nhom->find('list', array(
            'fields' => array('id', 'tennhom'),
            'conditions' => array(
                'Nhom.trangthai' => 'hoatdong'
            ),
            'contain' => false
        ));

        $this->loadModel( 'Menu' );
        $arr_Menu_level0 = $this->Menu->find('list', array(
            'fields' => array('id', 'tenmenu'),
            'conditions' => array(
                'Menu.trangthai' => 'hoatdong',
                'Menu.menucha'  => 0
            ),
            'contain' => false
        ));

        if(isset( $this->data['hidden_nguoidung_nhom_id'] ))
        {
            $this->loadModel( 'Nguoidung' );
            $arr_Nguoidung = $this->Nguoidung->find('list', array(
                'fields' => array('id', 'tendangnhap'),
                'conditions' => array(
                    'Nguoidung.trangthai' => 'hoatdong'
                ),
                'contain' => false
            ));


            $arr_NguoidungNhom = $this->NguoidungNhom->find('all', array(
                'fields' => array('Nhom.id', 'Nhom.tennhom', 'Nguoidung.id', 'Nguoidung.tendangnhap' ),
                'conditions' => array(
                    'Nhom.trangthai' => 'hoatdong',
                    'Nguoidung.trangthai' => 'hoatdong',
                    'nhomid' => $this->data['hidden_nguoidung_nhom_id']
                ),
                'contain' => false
            ));

            $arr_list_id_Nguoidung = array();


            foreach ($arr_NguoidungNhom as $value) {
                $arr_list_id_Nguoidung = $arr_list_id_Nguoidung + array( $value['Nguoidung']['id'] => $value['Nguoidung']['tendangnhap'] );
            }

            $arr_list_id_Nguoidungchuphanquyen = array_diff($arr_Nguoidung, $arr_list_id_Nguoidung);

            $this->set( 'arr_list_id_Nguoidung', $arr_list_id_Nguoidung );
            $this->set( 'arr_list_id_Nguoidungchuphanquyen', $arr_list_id_Nguoidungchuphanquyen );
            $this->set( 'nguoidung_nhom_id',  $this->data['hidden_nguoidung_nhom_id']);
        }

        $this->set( 'arr_Nhom', $arr_Nhom );
        $this->set( 'arr_Menu_level0', $arr_Menu_level0 );

        if( $this->Session->check('Quyen_nhomid') && $this->Session->check('Quyen_menuid') ){
            $this->loadModel( 'Menu' );
            $arr_Menu = $this->Menu->find('all', array(
                'conditions' => array(
                    'Menu.trangthai' => 'hoatdong',
                    'Menu.menucha'  => $this->Session->read('Quyen_menuid')
                ),
                'contain' => false
            ));
            $arr_Quyen = $this->Quyen->find('all', array(
                'conditions' => array(
                    'Quyen.trangthai' => 'hoatdong',
                    'Quyen.nhomid'  => $this->Session->read('Quyen_nhomid')
                ),
                'contain' => false
            ));
            $this->set( 'arr_Menu', $arr_Menu );
            $this->set( 'arr_Quyen', $arr_Quyen );
            $this->Session->delete('Quyen_nhomid');
            $this->Session->delete('Quyen_menuid');
            $this->set( 'session', 1 );
        }
        $this->layout = 'admin';

    }

    public function admin_loadphanhe( $nhom_id = null ,$phanhe_id = null ){
        $this->loadModel( 'Menu' );
        $arr_Menu = $this->Menu->find('all', array(
            'conditions' => array(
                'Menu.trangthai' => 'hoatdong',
                'Menu.menucha'  => $phanhe_id
            ),
            'contain' => false
        ));
        $arr_Quyen = $this->Quyen->find('all', array(
            'conditions' => array(
                'Quyen.trangthai' => 'hoatdong',
                'Quyen.nhomid'  => $nhom_id
            ),
            'contain' => false
        ));
        $this->set( 'arr_Menu', $arr_Menu );
        $this->set( 'arr_Quyen', $arr_Quyen );
        $this->layout = 'ajax';
    }
}